package com.woniu.order.util;

import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.woniu.order.param.PagePayParam;
import lombok.Data;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Data
public class AlipayUtil {

    public static String alipayPublicKey ="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw8cYpnAQTuqxJldaxVKbMG45UM0yPYNRYKn4G9PLxRxu8OHl8UDXdk1C3D0ltrm9CaSr5PWhaslRX+NDJRXCURCUe00jCDWmM0enUxS8mnYuZ9kgprEy4EDZOoWwAnv3znhPzCdfcHEVOH00/5oxpapF+8dPAtHK1OGmsmkXGtGjctrrYZyAkmaRUDkvMC9WJnKaTVbRcSw9vEkkGPgqIA8k+gfLp0G7GSQoDd+qM25VZgGy862d2MYJup4etps0n4lu0m6BqKkYDnogk+7hkowfDQr6tYRmfDdsDfuweZZyixBYfZ9DL5ZRXGfykOfGZB6PTxEVnglcTcpU4jUpwwIDAQAB";

    /**
     * 网页支付方法 参数:封装支付参数，需要包含订单号、支付金额、支付说明、详细描述（可选）、同步回调地址、异步回调地址
     */
    public String pagePay(PagePayParam param) throws Exception {
        // 实例化网页支付请求对象
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();



        // 设置参数
        String content = "{\"out_trade_no\":\""+ param.getOrderId() +"\","
                + "\"total_amount\":\""+ param.getMoney() +"\","
                + "\"subject\":\""+ param.getSubject() +"\","
                + "\"body\":\""+ param.getBody() +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}";
        request.setBizContent(content);
        //设置同步回调和异步回调
        request.setReturnUrl(param.getReturnUrl());
        request.setNotifyUrl(param.getNotifyUrl());
        //执行网页支付请求
        AlipayTradePagePayResponse response = config().pageExecute(request);
        //判断支付宝响应结果
        if(response.isSuccess()) {
            //返回支付宝响应的支付表单html内容
            return response.getBody();
        }
        throw new Exception("发起支付失败");

    }
    public AlipayClient config() throws Exception {
        System.out.println("===================appid==================");
        AlipayConfig alipayConfig = new AlipayConfig();
        //设置网关地址
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        //设置应用ID
        alipayConfig.setAppId("9021000135692405");
        //设置应用私钥
        alipayConfig.setPrivateKey("MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCxvXo51qR7rmgH9ZBaP5qYplU3bLa6RAVgR/WAbKB+cidkySIVLOf/xva96rAA3keZWU22vf3XK7NbHwDVMTAiEE5gOtJFdD7lCGbYuq9b9g/uB68joJVNaFYOC7wx0+vwNlM/Jec81sTZ5eaxgYxgxhypBSNiCXcPckOy3kVUCrzddn8wi6B8YEgfr0ph2fWDvPzD6ZhhocyFxM5TU4B7nFvvz9i5knVezXj9RhttprnT6+KyJmwk5lqFqv2D172isU7BLGKkcFKBmeLo8BJ47B5fxeSdxGbS2Rw2KRmu8keAwxbo4x/MdSZXYzAQ0QIKmAambBBUX2oTjpu+dscHAgMBAAECggEBAKi7wDT4V94yjABCV2/VweX4wh2Dz1S1DoWAysfIV/SyWkOLzhdcMkLXl5ZkHgI1ArPJ28UISNrkWbi/jgLWV2N/J2ijbssQuESl3T0SvTcIOcc29EVWRWbfDhL3gQmsree1KwJf8U4IdYiGgKMy4Rld+gdbIWQIqEa5qxs7d4V3sSQ1itZF95MedymerP9H2MtBm9M75H80igdh8rPEYhjhaIjxnD3g/ZO89nqmPPdt6wWFeA5pNTVMObZCsaRDfEBfeTC4eVyrNIr/DJnxlQI9a93w3zXtM5MG1RlBQdWnx/Cy1pA2vWv5T5FjQdRLHqqjRNlhYs61DmD0s9PSHwECgYEA/UsqVKMkbHSLmMR8wwwpe93Lrvt/v4sp+uQZa8OjZpmiH+Iw1XbjlukkEzaGnI1IVlWUP3zbjp2v+5gTFwleu0sFSYbmb00bN/bAIlF/tShJ5IoXI6Nfq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7GSQoDd+qM25VZgGy862d2MYJup4etps0n4lu0m6BqKkYDnogk+7hkowfDQr6tYRmfDdsDfuweZZyixBYfZ9DL5ZRXGfykOfGZB6PTxEVnglcTcpU4jUpwwIDAQAB");
        //设置请求格式固定值json
        alipayConfig.setFormat("JSON");
        //设置字符集
        alipayConfig.setCharset("UTF-8");
        //设置签名类型
        alipayConfig.setSignType("RSA2");
        //实例化客户端
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        return alipayClient;

//        //<-- 请填写您的应用私钥，例如：MIIEvQIBADANB ... ... -->
//        String privateKey  = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDy6nixXnMGcQXsqpigjbjktfyPz1yqVadFEFITyDa9NRGqLE1ruydYf3aWhL92uHNGLu36FXvhovUXc7+IUjf6RAjpOHyvtWd/L8DELaFo3QX9yeCrVJy2J0ILJCWNlHFJCxpT39ZvJPgBR6Y/s+woND72hjvvxvXa3df8SnysphDn3WO7drY3QeeMJQw6IyNHxzplNyFOGkMLKdhy1D234T6WNAP7Dnr/4iRZ4sPzs8w+xaio5aVoSBU/A9AtyLZlTgbP7277R+iB01mFKCzSoXus1gZG5oKz5VvOMPXKxlU8o6TAc+VxLA1vjYqbTORJbjGH7R3uEyX/1rW+h7+RAgMBAAECggEAZ9ICp3wAmV4w9Aetl+E6hrWpLiWoKzzUctYY3k+R5agK1rp8lHC2zkIcAxTVZ/5JdlOUbnBENBmbutxDnhXVyubNEDCOSeK0S02zi6Uc+V6NsvxXQ6K0H74DohNe4XXyPQ+7vuFKGtKoFJP04AEcC8rYpjCU6VQVu+dY6oFDa93w4++QxUIi1no94r6q9zGY/9XiEYQLYVKkvQcY6rQF9P3x7l8LZ/uxjsqfDY4ki0z62bRV7SzzhsoAX9hzXwRI7PRN9kuS3a9rTxsls8VOmGJbkaPAp81u6zN7th0NijjzMg36i1o+oZwtA+lRsCLKz4S4yAEYB6OcghVv8nlXYQKBgQD+PfJQ0Qvtebxukleqn4dCO1mmAuamzQPKOQBPZWU0Gmu7SJzUNohKcdqg5mdSCD4K7bPk3ErXa6/paNIxN/htU+HEWkGtDcmhi4RoAKQ+0hGylKvn5U6qFKmUoCnxaJvrKPQCGPY5w4QDmuiwzvwp6s4cmLyZXeNAXxyYM6/8jQKBgQD0mHnKk9IBfdhY2amTMlba6XOvjWpolAGrcGNtEaEd0pRJX5rc3Lc8prRhlAIbWsjZ11G1dFzxAxHO/4P50AifVaErYshFH8uEFTuKtQTi0XKOup4rp+4+y/7dC552neEaE/fCcqgmzurJzEAw4pfhVogre/VrwG3csS3EfKooFQKBgEwLFzkY7apf6P5hgxsjj/89ZTkEURy+4U1Q80jMGRMWe/aOZ2oJzYYGu8+4Wlrs4DHAmzfus+vu57S2h3f1N7brifGC1XX/U7mf00CQQJARoP16/lfj/D1e5MHxjdoI4enaW/FsqejpE8lIQiTwI/gdpPxkqrRsopYUXKchfpfZAoGBAO6AP0HpIb2SmIMKuu9tBoQvXz6vBjMzHu2siUaxCgmqbC2fq8Hw9juN0hXT3LoPrAm+i04ESRw5ggP14xqUX9P+C0ni6KV+e1Qv3/9Aw6Ln2GyUS+cFuKpDACFKUxoWmIh1MAHLC2vMF+Tt5SnFyZNQbTb5iDWpTXcEzTvq04CBAoGAY6NX9W3hrfHf7AFDPCC+weB+4vnrfuruPM4Uva60RNzQy0lPq6DI0VMtuWHERqQfrFqD82tB9+8yhNqstHOpwHlOMIkKuNmYZc8Qq4AglDj7mkT0Xn4AUhbuqlms2XvMkHDl1mCb7eUZAt8PExRmM1MGwaR5ypsEsVVwEXgfprk=";
//        AlipayConfig alipayConfig = new AlipayConfig();
//        alipayConfig.setPrivateKey(privateKey);
//        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
////        <-- 请填写您的AppId，例如：2019091767145019 -->
//        alipayConfig.setAppId("9021000135690694");
//        alipayConfig.setCharset("UTF-8");
//        alipayConfig.setSignType("RSA2");
//        alipayConfig.setFormat("json");
////        <-- 请填写您的应用公钥证书文件路径，例如：/foo/appCertPublicKey_2019051064521003.crt -->
//        alipayConfig.setAppCertPath("D:/Desk/aliyunPublic/appPublicCert.crt");
////        <-- 请填写您的支付宝公钥证书文件路径，例如：/foo/alipayCertPublicKey_RSA2.crt -->
//        alipayConfig.setAlipayPublicCertPath("D:/Desk/aliyunPublic/alipayPublicCert.crt");
////        <-- 请填写您的支付宝根证书文件路径，例如：/foo/alipayRootCert.crt -->
//        alipayConfig.setRootCertPath("D:/Desk/aliyunPublic/alipayRootCert.crt");
//        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
//        return alipayClient;
    }






}
